package com.hjh.testdemo.ui.video.play

import android.net.Uri
import androidx.core.net.toUri
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer
import com.hjh.testdemo.databinding.FragmentVideoPlayerBinding
import com.hjh.core.activity.BaseFragment

/**
 * @author： hjh
 * @createDate： 2025/6/4 19:05
 * @description：
 * @qq： 3472903821
 * @version: 1.0
 */
class VideoPlayerFragment : BaseFragment<FragmentVideoPlayerBinding>() {

    private var player: ExoPlayer? = null

    override fun initView() {
        // 从 Navigation 参数获取视频 URI
        val videoUri = arguments?.getString("videoUri")?.toUri()
        if (videoUri != null) {
            initializePlayer(videoUri)
        }
    }

    private fun initializePlayer(videoUri: Uri) {
        player = ExoPlayer.Builder(requireContext()).build()
        binding.playerView.player = player

        val mediaItem = MediaItem.fromUri(videoUri)
        player?.setMediaItem(mediaItem)
        player?.prepare()
        player?.playWhenReady = true
    }

    override fun onPause() {
        super.onPause()
        player?.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        player?.release()
    }
}